import {
    goodsSkuDetail,
    manjian,
    aftersale,
    modifyClicks,
    addGoodsbrowse,
    goodsSkuInfo,
    goodsQrcode
} from "@/api/goods/goods"
import {
    sharegoodsSkuDetail,
} from "@/api/memberpayment/goods"
import {
    isCollect,
    addCollect,
    deleteCollect
} from "@/api/goods/goods_collect"
import {
    couponTypeList,
    couponReceive
} from "@/api/coupon"
import {
    bundlingList
} from "@/api/bundling"
import {
    goodsEvaluateList
} from "@/api/goods/evaluate"
import {
    mapGetters
} from "vuex"
import CountDown from "vue2-countdown"
import {
    shopIsSubscribe,
    addShopSubscribe,
    deleteShopSubscribe
} from "@/api/shop"
import {
    getArea
} from "@/api/address"
import {
    getMemberId
} from "@/api/member/info"
import config from "@/utils/config"
import $ from "../../../api/collectinfo/jquery-3.1.1";


export default {
    data: () => {
        return {
            skuId: 0,
            loading: true,

            picZoomUrl: "",
            thumbPosition: 0,
            // 是否可以移动
            moveThumbLeft: false,
            moveThumbRight: false,

            // 商品详情
            goodsSkuDetail: {
                video_url: ""
            },
            discountText: "距离结束仅剩",
            discountTimeMachine: {
                currentTime: 0,
                startTime: 0,
                endTime: 0
            },
            qrcode: "",
            specDisabled: false,
            specBtnRepeat: false, //防止重复
            btnSwitch: false,

            // 店铺详情
            shopInfo: {},
            whetherCollection: 0,
            score: 0,

            couponList: [], //优惠券列表
            couponBtnRepeat: false, //获取优惠券防止重复提交

            manjian: {}, //满减活动列表

            //评价
            currentPage: 1,
            pageSize: 10,
            total: 0,
            goodsEvaluateList: [],
            //组合套餐
            bundling: [{
                bundling_goods: {
                    bl_name: "",
                    sku_image: ""
                }
            }],
            service: null,

            number: 1,
            tabName: "detail",
            tabBundling: "",
            playerOptions: {
                playbackRates: [0.5, 1.0, 1.5, 2.0, 3.0], // 可选的播放速度
                autoplay: false, // 如果为true,浏览器准备好时开始回放。
                muted: false, // 默认情况下将会消除任何音频。
                loop: false, // 是否视频一结束就重新开始。
                preload: "auto", // 建议浏览器在<video>加载元素后是否应该开始下载视频数据。auto浏览器选择最佳行为,立即开始加载视频（如果浏览器支持）
                language: "zh-CN",
                aspectRatio: "16:9", // 将播放器置于流畅模式，并在计算播放器的动态大小时使用该值。值应该代表一个比例 - 用冒号分隔的两个数字（例如"16:9"或"4:3"）
                fluid: true, // 当true时，Video.js player将拥有流体大小。换句话说，它将按比例缩放以适应其容器。
                sources: [{
                    type: "video/mp4", // 类型
                    src: "" // url地址
                }],
                poster: "", // 封面地址
                notSupportedMessage: "此视频暂无法播放，请稍后再试", // 允许覆盖Video.js无法播放媒体源时显示的默认信息。
                controlBar: {
                    timeDivider: true, // 当前时间和持续时间的分隔符
                    durationDisplay: true, // 显示持续时间
                    remainingTimeDisplay: true, // 是否显示剩余时间功能
                    fullscreenToggle: true // 是否显示全屏按钮
                }
            },
            switchMedia: "img",

            // 是否关注店铺
            hasFollow: false,

            // 省市区县
            provinceArr: {},
            cityArr: {},
            districtArr: {},

            // 省市区县 id
            currTabAddres: "province",
            hideRegion: false,
            selectedAddress: {},
            day: 0,
            hr: 0,
            min: 0,
            sec: 0,
            end_time: '',
            start_time: '',
            //goods_contents: 0,
            discount_contents: '',
            rule_json: '',
            site_ids: '',
            member_id: '',
            source_member: -1,
            dialogVisible: false,
        }
    },
    components: {
        CountDown
    },
    created() {
        this.skuId = this.$route.params.pathMatch
        this.source_member = this.$route.query.source_member
        if (this.source_member) {
            this.getshareGoodsSkuDetail()
        } else {
            this.getGoodsSkuDetail()
        }
        this.getMemberIdInfo()
            //调用初始化
        $(function() {
            var topic_id, topic_goods_id;
            $.ajax({
                dataType: 'JSON',
                type: 'POST',
                url: "/api/topicbrowse/add",
                data: { topic_id: topic_id, topic_goods_id: topic_goods_id },
                success: function(res) {

                }
            });
            /**
             *获取网页来源的地址
             */
            function getReferrer() {
                var referrer = "";
                try {
                    referrer = window.top.document.referrer;
                } catch (e) {
                    if (window.parent) {
                        try {
                            referrer = window.parent.document.referrer;
                        } catch (e2) {
                            referrer = "";
                        }
                    }
                }
                if (referrer === "") {
                    referrer = document.referrer;
                }
                return referrer;
            }
            //统计用户访问网站页面时间
            var tjSecond = 0;
            var tjRandom = 0;
            //定时增加时间
            window.setInterval(function() {
                tjSecond++;
            }, 1000);
            //获取随机的时间
            tjRandom = (new Date()).valueOf();
            //加载
            window.onload = function() {
                var goods_sku_id;
            };
            //onbeforeunload 事件在即将离开当前页面（刷新或关闭）时触发
            //该事件可用于弹出对话框，提示用户是继续浏览页面还是离开当前页面
            window.onbeforeunload = function() {
                //定义空的容器
                var params = {};
                params.tjRd = tjRandom;
                //获取当前的url地址
                params.url = location.href;
                params.time = tjSecond;
                params.time_in = Date.parse(new Date()) - (tjSecond * 1000);
                params.time_out = Date.parse(new Date());
                //获取页面的title标题
                params.title = document.title;
                //获取域
                params.domain = document.domain;
                //获取屏幕的高度
                params.client_height = window.screen.height;
                //获取屏幕的宽度
                params.client_width = window.screen.width;
                //获取语言
                params.client_language = navigator.language;
                //获取来源地址
                params.refer = getReferrer();
                params.goods_sku_id = goods_sku_id;

                if (params.time > 0) {
                    $.ajax({
                        dataType: 'JSON',
                        type: 'POST',
                        url: "/api/pageretention/addretention",
                        data: { params: params },
                        success: function(res) {

                        }
                    });
                }


            };
        });
    },
    computed: {
        ...mapGetters(["token", "defaultHeadImage", "defaultShopImage", "defaultGoodsImage", "addonIsExit", 'locationRegion'])
    },
    mounted: function() {
        this.countdown()
    },
    watch: {
        $route: function(curr) {
            this.skuId = curr.params.pathMatch
            this.getGoodsSkuDetail()
        }
    },
    methods: {
        handleClose(done) {
            this.$confirm('确认关闭？')
                .then(_ => {
                    done();
                })
                .catch(_ => {});
        },
        countdown: function() {
            // 定义结束时间戳
            var x = this.$timeStampTurnTime(this.goodsSkuDetail.end_time)
            const end = Date.parse(new Date(x))
                // 定义当前时间戳
            const now = Date.parse(new Date())

            // 做判断当倒计时结束时都为0
            if (now >= end) {
                this.day = 0
                this.hr = 0
                this.min = 0
                this.sec = 0
                return
            }
            // 用结束时间减去当前时间获得倒计时时间戳
            const msec = end - now
            let day = parseInt(msec / 1000 / 60 / 60 / 24) //算出天数
            let hr = parseInt(msec / 1000 / 60 / 60 % 24) //算出小时数
            let min = parseInt(msec / 1000 / 60 % 60) //算出分钟数
            let sec = parseInt(msec / 1000 % 60) //算出秒数
                //给数据赋值
            this.day = day
            this.hr = hr > 9 ? hr : '0' + hr
            this.min = min > 9 ? min : '0' + min
            this.sec = sec > 9 ? sec : '0' + sec

            //定义this指向
            const that = this
                // 使用定时器 然后使用递归 让每一次函数能调用自己达到倒计时效果
            setTimeout(function() {
                that.countdown()
            }, 1000)
        },


        tabChange(tab, event) {},
        bundlingChange(tab, event) {},
        getshareGoodsSkuDetail() {
            sharegoodsSkuDetail({
                    sku_id: this.skuId,
                    share_member_id: this.source_member,
                })
                .then(res => {
                    let data = res.data
                    this.site_ids = res.data.shop_info.site_id;

                    if (data.goods_sku_detail != null) {


                        this.goodsSkuDetail = data.goods_sku_detail


                        if (this.goodsSkuDetail.promotion_type == 'ladderdiscount' || this.goodsSkuDetail.promotion_type == 'nthmfolddiscount' && data.goods_sku_detail.promotion_info) {
                            this.discount_contents = JSON.parse(data.goods_sku_detail.promotion_info.info.discount_content)
                                // this.discount_contents = data.goods_sku_detail.promotion_info.info.discount_content
                        }
                        if (this.goodsSkuDetail.promotion_type == 'platformmanjian' && data.goods_sku_detail.promotion_info) {
                            this.rule_json = JSON.parse(data.goods_sku_detail.promotion_info.info.rule_json)
                                // this.discount_contents = data.goods_sku_detail.promotion_info.info.discount_content
                        }
                        // this.goods_contents = this.goodsSkuDetail.promotion_info.info.discount_content
                        // this.goods_contents = JSON.parse(this.goods_contents)
                        // console.log(this.goods_contents)


                        this.shopInfo = data.shop_info
                        let num = (Number(this.shopInfo.shop_desccredit) + Number(this.shopInfo.shop_servicecredit) + Number(this.shopInfo
                            .shop_deliverycredit)) / 3
                        this.score = Number(num.toFixed(1));

                        if (this.skuId == 0) this.skuId = this.goodsSkuDetail.sku_id

                        this.goodsSkuDetail.sku_images = this.goodsSkuDetail.sku_images.split(",")

                        //媒体
                        if (this.goodsSkuDetail.video_url) {
                            this.switchMedia = "video"
                            this.playerOptions.poster = img(this.goodsSkuDetail.sku_images[0])
                            this.playerOptions.sources[0].src = img(this.goodsSkuDetail.video_url)
                        }

                        this.picZoomUrl = this.goodsSkuDetail.sku_images[0]

                        this.goodsSkuDetail.unit = this.goodsSkuDetail.unit || "件"

                        // 当前商品SKU规格
                        if (this.goodsSkuDetail.sku_spec_format) this.goodsSkuDetail.sku_spec_format = JSON.parse(this.goodsSkuDetail.sku_spec_format)

                        // 商品属性
                        if (this.goodsSkuDetail.goods_attr_format) {
                            let goods_attr_format = JSON.parse(this.goodsSkuDetail.goods_attr_format)
                            this.goodsSkuDetail.goods_attr_format = JSON.parse(this.goodsSkuDetail.goods_attr_format)
                            this.goodsSkuDetail.goods_attr_format = util.unique(this.goodsSkuDetail.goods_attr_format, "attr_id")
                            for (var i = 0; i < this.goodsSkuDetail.goods_attr_format.length; i++) {
                                for (var j = 0; j < goods_attr_format.length; j++) {
                                    if (this.goodsSkuDetail.goods_attr_format[i].attr_id == goods_attr_format[j].attr_id && this.goodsSkuDetail.goods_attr_format[
                                            i].attr_value_id != goods_attr_format[j].attr_value_id) {
                                        this.goodsSkuDetail.goods_attr_format[i].attr_value_name += "、" + goods_attr_format[j].attr_value_name
                                    }
                                }
                            }
                        }

                        // 商品SKU格式
                        if (this.goodsSkuDetail.goods_spec_format) this.goodsSkuDetail.goods_spec_format = JSON.parse(this.goodsSkuDetail
                            .goods_spec_format)

                        window.document.title = `${this.goodsSkuDetail.sku_name} - ${window.document.title}`

                        // 限时折扣
                        if ((this.addonIsExit.discount && this.goodsSkuDetail.promotion_type == 1) ||
                            (this.addonIsExit.ladderdiscount && this.goodsSkuDetail.promotion_type == 'ladderdiscount') ||
                            (this.addonIsExit.nthmfolddiscount && this.goodsSkuDetail.promotion_type == 'nthmfolddiscount') ||
                            (this.addonIsExit.platformmanjian && this.goodsSkuDetail.promotion_type == 'platformmanjian')) {
                            //检测倒计时
                            if (this.goodsSkuDetail.end_time - res.timestamp > 0) {
                                this.discountTimeMachine = {
                                    currentTime: res.timestamp,
                                    startTime: res.timestamp,
                                    endTime: this.goodsSkuDetail.end_time
                                }
                            } else {
                                this.goodsSkuDetail.promotion_type = 0
                            }
                        }
                        this.loading = false
                    } else {
                        this.$router.push("/")
                    }
                })
                .then(res => {
                    if (this.token != "") {
                        this.getWhetherCollection()
                        this.isFollow()
                    }
                    if (this.addonIsExit.manjian) this.getManjian()
                    if (this.addonIsExit.coupon) this.getCoupon()
                    if (this.addonIsExit.bundling) this.getBundling()
                    this.getAftersale()
                    this.modifyGoodsInfo()
                    this.getGoodsEvaluate()
                    this.getGoodsQrcode()
                    this.getAddress('province', null, true);
                    if (!this.locationRegion) {

                        this.$store.commit("app/SET_LOCATION_REGION", {
                            "level_1": {
                                "id": 110000,
                                "pid": 0,
                                "name": "北京市",
                                "shortname": "北京",
                                "longitude": "116.40529",
                                "latitude": "39.904987",
                                "level": 1,
                                "sort": 1,
                                "status": 1,
                                "default_data": 1
                            },
                            "level_2": {
                                "id": 110100,
                                "pid": 110000,
                                "name": "北京市",
                                "shortname": "北京",
                                "longitude": "116.40529",
                                "latitude": "39.904987",
                                "level": 2,
                                "sort": 1,
                                "status": 1,
                                "default_data": 1
                            },
                            "level_3": {
                                "id": 110101,
                                "pid": 110100,
                                "name": "东城区",
                                "shortname": "东城",
                                "longitude": "116.418755",
                                "latitude": "39.917545",
                                "level": 3,
                                "sort": 3,
                                "status": 1,
                                "default_data": 1
                            }
                        });
                    }

                    this.selectedAddress = this.locationRegion;
                    this.provinceId = this.selectedAddress.level_1.id
                    this.getAddress('city', null, true, () => {
                        this.cityId = this.selectedAddress.level_2.id
                        if (this.cityId) this.getAddress('district', null, true)
                    });
                })
                .catch(res => {
                    this.loading = false
                    this.$router.push("/")
                })
        },
        getGoodsSkuDetail() {
            goodsSkuDetail({
                    sku_id: this.skuId
                })
                .then(res => {
                    let data = res.data
                    this.site_ids = res.data.shop_info.site_id;

                    if (data.goods_sku_detail != null) {


                        this.goodsSkuDetail = data.goods_sku_detail


                        if (this.goodsSkuDetail.promotion_type == 'ladderdiscount' || this.goodsSkuDetail.promotion_type == 'nthmfolddiscount' && data.goods_sku_detail.promotion_info) {
                            this.discount_contents = JSON.parse(data.goods_sku_detail.promotion_info.info.discount_content)
                                // this.discount_contents = data.goods_sku_detail.promotion_info.info.discount_content
                        }
                        if (this.goodsSkuDetail.promotion_type == 'platformmanjian' && data.goods_sku_detail.promotion_info) {
                            this.rule_json = JSON.parse(data.goods_sku_detail.promotion_info.info.rule_json)
                                // this.discount_contents = data.goods_sku_detail.promotion_info.info.discount_content
                        }
                        // this.goods_contents = this.goodsSkuDetail.promotion_info.info.discount_content
                        // this.goods_contents = JSON.parse(this.goods_contents)
                        // console.log(this.goods_contents)


                        this.shopInfo = data.shop_info
                        let num = (Number(this.shopInfo.shop_desccredit) + Number(this.shopInfo.shop_servicecredit) + Number(this.shopInfo
                            .shop_deliverycredit)) / 3
                        this.score = Number(num.toFixed(1));

                        if (this.skuId == 0) this.skuId = this.goodsSkuDetail.sku_id

                        this.goodsSkuDetail.sku_images = this.goodsSkuDetail.sku_images.split(",")

                        //媒体
                        if (this.goodsSkuDetail.video_url) {
                            this.switchMedia = "video"
                            this.playerOptions.poster = img(this.goodsSkuDetail.sku_images[0])
                            this.playerOptions.sources[0].src = img(this.goodsSkuDetail.video_url)
                        }

                        this.picZoomUrl = this.goodsSkuDetail.sku_images[0]

                        this.goodsSkuDetail.unit = this.goodsSkuDetail.unit || "件"

                        // 当前商品SKU规格
                        if (this.goodsSkuDetail.sku_spec_format) this.goodsSkuDetail.sku_spec_format = JSON.parse(this.goodsSkuDetail.sku_spec_format)

                        // 商品属性
                        if (this.goodsSkuDetail.goods_attr_format) {
                            let goods_attr_format = JSON.parse(this.goodsSkuDetail.goods_attr_format)
                            this.goodsSkuDetail.goods_attr_format = JSON.parse(this.goodsSkuDetail.goods_attr_format)
                            this.goodsSkuDetail.goods_attr_format = util.unique(this.goodsSkuDetail.goods_attr_format, "attr_id")
                            for (var i = 0; i < this.goodsSkuDetail.goods_attr_format.length; i++) {
                                for (var j = 0; j < goods_attr_format.length; j++) {
                                    if (this.goodsSkuDetail.goods_attr_format[i].attr_id == goods_attr_format[j].attr_id && this.goodsSkuDetail.goods_attr_format[
                                            i].attr_value_id != goods_attr_format[j].attr_value_id) {
                                        this.goodsSkuDetail.goods_attr_format[i].attr_value_name += "、" + goods_attr_format[j].attr_value_name
                                    }
                                }
                            }
                        }

                        // 商品SKU格式
                        if (this.goodsSkuDetail.goods_spec_format) this.goodsSkuDetail.goods_spec_format = JSON.parse(this.goodsSkuDetail
                            .goods_spec_format)

                        window.document.title = `${this.goodsSkuDetail.sku_name} - ${window.document.title}`

                        // 限时折扣
                        if ((this.addonIsExit.discount && this.goodsSkuDetail.promotion_type == 1) ||
                            (this.addonIsExit.ladderdiscount && this.goodsSkuDetail.promotion_type == 'ladderdiscount') ||
                            (this.addonIsExit.nthmfolddiscount && this.goodsSkuDetail.promotion_type == 'nthmfolddiscount') ||
                            (this.addonIsExit.platformmanjian && this.goodsSkuDetail.promotion_type == 'platformmanjian')) {
                            //检测倒计时
                            if (this.goodsSkuDetail.end_time - res.timestamp > 0) {
                                this.discountTimeMachine = {
                                    currentTime: res.timestamp,
                                    startTime: res.timestamp,
                                    endTime: this.goodsSkuDetail.end_time
                                }
                            } else {
                                this.goodsSkuDetail.promotion_type = 0
                            }
                        }
                        this.loading = false
                    } else {
                        this.$router.push("/")
                    }
                })
                .then(res => {
                    if (this.token != "") {
                        this.getWhetherCollection()
                        this.isFollow()
                    }
                    if (this.addonIsExit.manjian) this.getManjian()
                    if (this.addonIsExit.coupon) this.getCoupon()
                    if (this.addonIsExit.bundling) this.getBundling()
                    this.getAftersale()
                    this.modifyGoodsInfo()
                    this.getGoodsEvaluate()
                    this.getGoodsQrcode()
                    this.getAddress('province', null, true);
                    if (!this.locationRegion) {

                        this.$store.commit("app/SET_LOCATION_REGION", {
                            "level_1": {
                                "id": 110000,
                                "pid": 0,
                                "name": "北京市",
                                "shortname": "北京",
                                "longitude": "116.40529",
                                "latitude": "39.904987",
                                "level": 1,
                                "sort": 1,
                                "status": 1,
                                "default_data": 1
                            },
                            "level_2": {
                                "id": 110100,
                                "pid": 110000,
                                "name": "北京市",
                                "shortname": "北京",
                                "longitude": "116.40529",
                                "latitude": "39.904987",
                                "level": 2,
                                "sort": 1,
                                "status": 1,
                                "default_data": 1
                            },
                            "level_3": {
                                "id": 110101,
                                "pid": 110100,
                                "name": "东城区",
                                "shortname": "东城",
                                "longitude": "116.418755",
                                "latitude": "39.917545",
                                "level": 3,
                                "sort": 3,
                                "status": 1,
                                "default_data": 1
                            }
                        });
                    }

                    this.selectedAddress = this.locationRegion;
                    this.provinceId = this.selectedAddress.level_1.id
                    this.getAddress('city', null, true, () => {
                        this.cityId = this.selectedAddress.level_2.id
                        if (this.cityId) this.getAddress('district', null, true)
                    });
                })
                .catch(res => {
                    this.loading = false
                    this.$router.push("/")
                })
        },
        changeThumbImg(tag) {
            if (this.goodsSkuDetail.sku_images.length < 4) return
            let page = this.goodsSkuDetail.sku_images.length % 4 // 可见数量4个
            let position = 88
            if (page == 0) page = this.goodsSkuDetail.sku_images.length - 4 // 可见数量4个
            else if (page != 0 && page != 1 && page < 2) return

            if (tag == "prev") {
                if (this.thumbPosition != 0 && Math.round(this.thumbPosition, 2) != position) {
                    this.thumbPosition += position
                        // this.moveThumbLeft = true;
                } else {
                    // this.moveThumbLeft = false;
                }
            } else if (tag == "next") {
                if (Math.round(this.thumbPosition, 2) != -Math.round(position * page, 2)) {
                    this.thumbPosition -= position
                        // this.moveThumbRight = true;
                } else {
                    // this.moveThumbRight = false;
                }
            }
        },
        getMemberIdInfo() {
            getMemberId()
                .then(res => {
                    if (res.code >= 0) {
                        this.member_id = res.data;
                    }
                })
                .catch(err => {
                    console.log(err)
                })
        },
        copyLink() {
            if (this.goodsSkuDetail.is_share !== 1) {
                this.dialogVisible = true;
            } else {
                var url = `${config.webDomain}web/sku-${this.skuId}?source_member=${this.member_id}`;
                this.$util.copy(url);
            }

        },
        continueShare() {
            var url = `${config.webDomain}web/sku-${this.skuId}`;
            this.$util.copy(url);
            this.dialogVisible = false;
        },
        //获取用户是否关注
        getWhetherCollection() {
            isCollect({
                goods_id: this.goodsSkuDetail.goods_id
            }).then(res => {
                this.whetherCollection = res.data
            })
        },
        editCollection() {
            //未关注添加关注
            if (this.whetherCollection == 0) {
                addCollect({
                    sku_id: this.skuId,
                    site_id: this.goodsSkuDetail.site_id,
                    goods_id: this.goodsSkuDetail.goods_id,
                    category_id: this.goodsSkuDetail.category_id,
                    sku_name: this.goodsSkuDetail.sku_name,
                    sku_price: this.goodsSkuDetail.discount_price,
                    sku_image: this.goodsSkuDetail.sku_image
                }).then(res => {
                    var data = res.data
                    if (data > 0) {
                        this.whetherCollection = 1
                        this.goodsSkuDetail.collect_num++
                    }
                })
            } else {
                //已关注取消关注
                deleteCollect({
                    goods_id: this.goodsSkuDetail.goods_id
                }).then(res => {
                    var data = res.data
                    if (data > 0) {
                        this.whetherCollection = 0
                        this.goodsSkuDetail.collect_num--
                    }
                })
            }
        },
        //获取满减信息
        getManjian() {
            manjian({
                goods_id: this.goodsSkuDetail.goods_id,
                site_id: this.goodsSkuDetail.site_id
            }).then(res => {
                let data = res.data
                if (data) {
                    this.manjian = data
                }
            })
        },
        // 获取优惠券
        getCoupon() {
            couponTypeList({
                goods_id: this.goodsSkuDetail.goods_id,
                site_id: this.site_ids,
            }).then(res => {
                let data = res.data
                if (data) {
                    this.couponList = []
                    for (let i = 0; i < data.length; i++) {
                        if (i > 4) break
                        this.couponList.push(data[i])
                    }
                }
            })
        },
        // 领取优惠券
        receiveCoupon(couponTypeId) {
            if (this.couponBtnRepeat) return
            this.couponBtnRepeat = true

            couponReceive({
                    coupon_type_id: couponTypeId,
                    site_id: this.goodsSkuDetail.site_id,
                    get_type: 2 //获取方式:1订单2.直接领取3.活动领取
                })
                .then(res => {
                    var data = res.data
                    let msg = res.message
                    if (res.code == 0) {
                        msg = "领取成功"
                    }
                    this.$message({
                        message: msg,
                        type: "success"
                    })
                    this.couponBtnRepeat = false
                })
                .catch(res => {
                    this.couponBtnRepeat = false
                })
        },
        // 组合套餐
        getBundling() {
            bundlingList({
                sku_id: this.skuId
            }).then(res => {
                if (res.data && res.data.length) {
                    this.bundling = res.data
                    this.tabBundling = "bundling_" + this.bundling[0].bl_id
                    for (var i = 0; i < this.bundling.length; i++) {
                        for (var j = 0; j < this.bundling[i].bundling_goods.length; j++) {
                            if (this.bundling[i].bundling_goods[j].sku_id == this.skuId) {
                                this.bundling[i].bundling_goods.splice(j, 1)
                            }
                        }
                    }
                }
            })
        },
        // 售后保障
        getAftersale() {
            aftersale({}).then(res => {
                if (res.code == 0 && res.data) {
                    let data = res.data.content
                    if (res.data.content) this.service = res.data.content
                }
            })
        },
        //更新商品信息
        modifyGoodsInfo() {
            //更新商品点击量
            modifyClicks({
                sku_id: this.skuId,
                site_id: this.goodsSkuDetail.site_id
            })

            //添加足迹
            addGoodsbrowse({
                goods_id: this.goodsSkuDetail.goods_id,
                sku_id: this.skuId,
                category_id: this.goodsSkuDetail.category_id,
                category_id_1: this.goodsSkuDetail.category_id_1,
                category_id_2: this.goodsSkuDetail.category_id_2,
                category_id_3: this.goodsSkuDetail.category_id_3,
                site_id: this.goodsSkuDetail.site_id
            })
        },
        // 商品二维码
        getGoodsQrcode() {
            goodsQrcode({
                sku_id: this.skuId
            }).then(res => {
                let data = res.data
                if (data.path.h5.img) this.qrcode = img(data.path.h5.img)
            })
        },
        // 商品评价列表
        getGoodsEvaluate() {
            goodsEvaluateList({
                page: this.currentPage,
                page_size: this.pageSize,
                goods_id: this.goodsSkuDetail.goods_id
            }).then(res => {
                let list = []
                let msg = res.message
                if (res.code == 0 && res.data) {
                    list = res.data.list
                    this.total = res.data.count
                }

                for (var i = 0; i < list.length; i++) {
                    // 1好评2中评3差评
                    if (list[i].explain_type == 1) {
                        list[i].star = 5
                    } else if (list[i].explain_type == 2) {
                        list[i].star = 3
                    } else if (list[i].explain_type == 3) {
                        list[i].star = 1
                    }
                    if (list[i].images) {
                        list[i].images = list[i].images.split(",")
                        list[i].imagesFormat = []
                        for (var k = 0; k < list[i].images.length; k++) {
                            list[i].imagesFormat.push(img(list[i].images[k]))
                        }
                    }

                    if (list[i].again_images) {
                        list[i].again_images = list[i].again_images.split(",")
                        list[i].againImagesFormat = []
                        for (var j = 0; j < list[i].again_images.length; j++) {
                            list[i].againImagesFormat.push(img(list[i].again_images[j]))
                        }
                    }
                    if (list[i].is_anonymous == 1) list[i].member_name = list[i].member_name.replace(list[i].member_name.substring(1,
                        list[i].member_name.length - 1), "***")
                }
                this.goodsEvaluateList = list
            })
        },
        // 图片加载失败
        imageErrorEvaluate(index) {
            this.goodsEvaluateList[index].member_headimg = this.defaultHeadImage
        },
        handlePageSizeChange(size) {
            this.pageSize = size
            this.getGoodsEvaluate()
        },
        handleCurrentPageChange(page) {
            this.currentPage = page
            this.getGoodsEvaluate()
        },
        changeSpec(skuId, spec_id) {
            if (this.specDisabled) return
            this.specBtnRepeat = false
            this.skuId = skuId
                // 清空选择
            for (var i = 0; i < this.goodsSkuDetail.goods_spec_format.length; i++) {
                var sku = this.goodsSkuDetail.goods_spec_format[i]
                for (var j = 0; j < sku.value.length; j++) {
                    // 排除当前点击的规格值
                    if (spec_id == this.goodsSkuDetail.goods_spec_format[i].value[j].spec_id) {
                        this.goodsSkuDetail.goods_spec_format[i].value[j].selected = false
                    }
                }
            }

            goodsSkuInfo({
                sku_id: this.skuId
            }).then(res => {
                let data = res.data
                if (data != null) {
                    data.sku_images = data.sku_images.split(",")

                    this.picZoomUrl = data.sku_images[0]

                    this.playerOptions.poster = img(data.sku_images[0])

                    // 当前商品SKU规格
                    if (data.sku_spec_format) data.sku_spec_format = JSON.parse(data.sku_spec_format)

                    // 商品SKU格式
                    if (data.goods_spec_format) data.goods_spec_format = JSON.parse(data.goods_spec_format)

                    this.keyInput(true)

                    // 限时折扣
                    if (data.promotion_type == 1) {
                        this.discountTimeMachine = {
                            currentTime: res.timestamp,
                            startTime: res.timestamp,
                            endTime: data.end_time
                        }
                    }
                    this.specBtnRepeat = false
                    Object.assign(this.goodsSkuDetail, data)
                } else {
                    this.$router.push("/")
                }
            })
        },
        changeNum(tag) {
            if (this.goodsSkuDetail.stock == 0) return

            var stock = this.goodsSkuDetail.stock
            var min = 1

            if (tag == "+") {
                // 加
                if (this.number < stock) {
                    this.number++
                } else {
                    return
                }
            } else if (tag == "-") {
                // 减
                if (this.number > min) {
                    this.number -= 1
                } else {
                    return
                }
            }
        },
        blur() {
            let newNumber = parseInt(this.number)
            this.number = 0
            setTimeout(() => {
                this.number = newNumber
            }, 0)
        },
        //输入数量
        keyInput(flag, callback) {
            setTimeout(() => {
                var stock = this.goodsSkuDetail.stock

                // 库存为0
                if (this.goodsSkuDetail.stock == 0) {
                    this.number = 0
                    return
                }

                // 防止空
                if (flag && this.number.length == 0) this.number = 1

                // 防止输入0和负数、非法输入
                if (flag && (this.number <= 0 || isNaN(this.number))) this.number = 1

                if (this.number > stock) {
                    this.number = stock
                }

                if (flag) this.number = parseInt(this.number)
                if (callback) callback()
            }, 0)
        },
        // 播放回调
        onPlayerPlay(player) {},
        // 暂停回调
        onPlayerPause(player) {},
        // 视频播完回调
        onPlayerEnded(player) {},
        // DOM元素上的readyState更改导致播放停止
        onPlayerWaiting(player) {},
        // 已开始播放回调
        onPlayerPlaying(player) {},
        // 当播放器在当前播放位置下载数据时触发
        onPlayerLoadeddata(player) {},
        // 当前播放位置发生变化时触发。
        onPlayerTimeupdate(player) {},
        //媒体的readyState为HAVE_FUTURE_DATA或更高
        onPlayerCanplay(player) {},
        //媒体的readyState为HAVE_ENOUGH_DATA或更高。这意味着可以在不缓冲的情况下播放整个媒体文件。
        onPlayerCanplaythrough(player) {},
        //播放状态改变回调
        playerStateChanged(playerCurrentState) {},
        //将侦听器绑定到组件的就绪状态。与事件监听器的不同之处在于，如果ready事件已经发生，它将立即触发该函数。。
        playerReadied(player) {},
        // 加入购物车
        joinCart() {
            //纠正数量
            this.keyInput(true, () => {
                if (this.goodsSkuDetail.stock == 0) {
                    this.$message({
                        message: "商品已售罄",
                        type: "warning"
                    })
                    return
                }

                if (this.number.length == 0 || this.number == 0) {
                    this.$message({
                        message: "购买数量不能为0",
                        type: "warning"
                    })
                    return
                }

                if (this.btnSwitch) return
                this.btnSwitch = true

                this.$store
                    .dispatch("cart/add_to_cart", {
                        site_id: this.goodsSkuDetail.site_id,
                        sku_id: this.goodsSkuDetail.sku_id,
                        num: this.number
                    })
                    .then(res => {
                        var data = res.data
                        if (data > 0) {
                            this.$message({
                                message: "加入购物车成功",
                                type: "success"
                            })
                        }
                        this.btnSwitch = false
                    })
            })
        },
        // 立即购买
        buyNow() {
            //纠正数量
            this.keyInput(true, () => {
                if (this.goodsSkuDetail.stock == 0) {
                    this.$message({
                        message: "商品已售罄",
                        type: "warning"
                    })
                    return
                }

                if (this.number.length == 0 || this.number == 0) {
                    this.$message({
                        message: "购买数量不能为0",
                        type: "warning"
                    })
                    return
                }

                var data = {
                    sku_id: this.skuId,
                    num: this.number
                }

                var data1 = {
                    sku_id: this.skuId,
                    num: this.number,
                    share_member_id: this.source_member,
                }
                if (this.source_member) {
                    this.$store.dispatch("order/setOrderCreateData", data1)
                    this.$router.push({
                        path: "/memberpayment/payment"
                    })
                } else {
                    this.$store.dispatch("order/setOrderCreateData", data)
                    this.$router.push({
                        path: "/payment"
                    })
                }
            })
        },
        countDownS_cb() {},
        countDownE_cb() {
            this.discountText = "活动已结束"
        },
        //是否关注
        isFollow() {
            shopIsSubscribe({
                site_id: this.goodsSkuDetail.site_id
            }).then(res => {
                if (res.code == 0) {
                    this.hasFollow = res.data
                }
            })
        },
        follow() {
            if (this.hasFollow) {
                deleteShopSubscribe({
                    site_id: this.goodsSkuDetail.site_id
                }).then(res => {
                    if (res.code == 0 && res.data) {
                        this.hasFollow = !this.hasFollow
                        this.$message({
                            message: "取消成功",
                            type: "success"
                        })
                    }
                })
            } else {
                addShopSubscribe({
                    site_id: this.goodsSkuDetail.site_id
                }).then(res => {
                    if (res.code == 0 && res.data) {
                        this.hasFollow = !this.hasFollow
                        this.$message({
                            message: "关注成功",
                            type: "success"
                        })
                    }
                })
            }
        },
        // 图片加载失败
        imageErrorSpec(index) {
            this.goodsSkuDetail.sku_images[index] = this.defaultGoodsImage
            this.picZoomUrl = this.defaultGoodsImage
        },
        /**
         * 获取地址
         * @param {Object} type
         * @param {Object} item
         * @param {Object} first 是否第一次
         */
        getAddress(type, item, first, callback) {
            let pid = 0
            switch (type) {
                case 'province':
                    //加载省
                    pid = 0
                    break
                case 'city':
                    //加载市
                    if (item) {
                        this.provinceId = item.id
                    }
                    pid = this.provinceId
                    this.cityArr = {}
                    this.districtArr = {}
                    break
                case 'district':
                    //加载区县
                    if (item) this.cityId = item.id
                    pid = this.cityId
                    this.districtArr = {}
                    break
            }
            if (item) {
                if (item.level <= 2) {
                    let len = item.level;
                    for (let i = len; i <= 3; i++) {
                        delete this.selectedAddress['level_' + i];
                    }
                }
                this.selectedAddress['level_' + item.level] = item;
            }

            if (!first) this.$store.commit("app/SET_LOCATION_REGION", this.selectedAddress)
            this.$forceUpdate();
            if (type == 'community') {
                this.hideRegion = true;
                setTimeout(() => {
                    this.hideRegion = false;
                }, 10);
                return;
            }

            getArea({
                    pid: pid
                })
                .then(res => {
                    const {
                        code,
                        data
                    } = res;
                    if (data) {
                        switch (type) {
                            case 'province':
                                //加载省
                                this.provinceArr = data
                                break
                            case 'city':
                                //加载市
                                this.cityArr = data
                                break
                            case 'district':
                                //加载区县
                                this.districtArr = data
                                break
                        }
                        this.currTabAddres = type

                        if (callback) callback();
                    }
                })
                .catch(err => {})
        }
    }
}